package com.turnengine.client.local.action.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.action.enums.ActionConditionExecute;
import com.turnengine.client.local.action.enums.ActionConditionMandatory;
import com.turnengine.client.local.action.enums.ActionConditionType;
import com.turnengine.client.local.action.enums.ActionTargetType;
import java.io.IOException;

/**
 * The Action Condition Serializer.
 */
public class ActionConditionSerializer implements IActionConditionSerializer {

	@Override
	public IActionCondition readObject(IDataReader reader) throws IOException {

		// Read fields
		int actionId = reader.readInt();
		ActionTargetType targetType = reader.readEnum(ActionTargetType.class, true);
		ActionConditionType type = reader.readEnum(ActionConditionType.class, true);
		ActionConditionExecute execute = reader.readEnum(ActionConditionExecute.class, true);
		ActionConditionMandatory mandatory = reader.readEnum(ActionConditionMandatory.class, true);
		int conditionId = reader.readInt();
		long conditionAmount = reader.readLong();

		// Create object
		IActionCondition object = new ActionCondition();
		object.setActionId(actionId);
		object.setTargetType(targetType);
		object.setType(type);
		object.setExecute(execute);
		object.setMandatory(mandatory);
		object.setConditionId(conditionId);
		object.setConditionAmount(conditionAmount);
		return object;
	}

	public void writeObject(IDataWriter writer, IActionCondition object) throws IOException {

		// Get fields
		int actionId = object.getActionId();
		ActionTargetType targetType = object.getTargetType();
		ActionConditionType type = object.getType();
		ActionConditionExecute execute = object.getExecute();
		ActionConditionMandatory mandatory = object.getMandatory();
		int conditionId = object.getConditionId();
		long conditionAmount = object.getConditionAmount();

		// Write fields
		writer.writeInt(actionId);
		writer.writeEnum(targetType, true);
		writer.writeEnum(type, true);
		writer.writeEnum(execute, true);
		writer.writeEnum(mandatory, true);
		writer.writeInt(conditionId);
		writer.writeLong(conditionAmount);
	}
}
